package ElectricCraft.frames;

import ElectricCraft.model.ChipModel;
import ElectricCraft.model.ModelGroup;
import ElectricCraft.model.ModelPackage;

import javax.swing.tree.DefaultMutableTreeNode;

/**
* Package view node tree. Automatically build node hierarchy.
*/
public class PackageViewNode extends DefaultMutableTreeNode {
    public final ModelGroup group;
    public final ChipModel model;

    public PackageViewNode(ModelGroup group, ChipModel model) {
        super(group.name);

        this.group = group;
        this.model = model;

        init();
    }

    public PackageViewNode(ModelPackage modelPackage) {
        this.group = modelPackage.groups;
        this.model = null;

        init();
    }

    @Override
    public String toString() {
        if (model == null) {
            return group.name;
        }
        return model.name;
    }

    private void init() {
        if (model != null) {
            return ;
        }

        for (ModelGroup subGroup : group.groups) {
            this.add(new PackageViewNode(subGroup, null));
        }

        for (ChipModel mdl : group.models) {
            this.add(new PackageViewNode(group, mdl));
        }
    }

    @Override
    public boolean isLeaf() {
        return model != null;
    }
}
